package com.zhongli.devplatform.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.po.common.ReadClazzGradePO;
import com.zhongli.devplatform.po.common.ReadGradePO;
import com.zhongli.devplatform.po.user.ReadUserPO;
import com.zhongli.devplatform.query.sys.GradeQuery;
import com.zhongli.devplatform.service.imp.common.ReadClazzGradeService;
import com.zhongli.devplatform.service.imp.common.ReadGradeService;
import com.zhongli.devplatform.service.imp.user.ReadUserService;
import com.zhongli.devplatform.vo.common.GradeVO;
import com.zhongli.devplatform.bean.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wy
 * @since 2020/7/29
 */
@Api(tags = "年级app接口",value = "年级app接口")
@RestController
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private ReadGradeService gradeService;

    @Autowired
    private ReadUserService userService;

    @Autowired
    private ReadClazzGradeService clazzGradeService;

    @ApiOperation("根据当前老师id查询出所教授的年级")
    @GetMapping("/allGradeInfoByTeacher")
    public Res getAllGradeByTeacherId(GradeQuery gradeQuery) {
        List<GradeVO> data = gradeService.getAllGradeByTeacherId(gradeQuery);
        return Res.ok(data);
    }


    /**
     *根据当用户查询出所有的历史年级信息
     * @return  所有的历史年级信息
     */
    @ApiOperation("根据当用户查询出所有的历史年级信息 -- 学生使用")
    @GetMapping("/getAllHistoryGrade")
    public Res<List<ReadGradePO>> getAllHistoryGrade(){
        Integer currentStuId = SecurityHelper.getCurrentUserId();
        ReadUserPO stu = userService.getById(currentStuId);
        List<ReadClazzGradePO> gradePOS = clazzGradeService.list(new QueryWrapper<ReadClazzGradePO>()
                .eq("clazz_id", stu.getClazzId()));
        List<Integer> collect = gradePOS
                .stream()
                .map(ReadClazzGradePO::getGradeId)
                .collect(Collectors.toList());
        if (collect.isEmpty()) return Res.not_ok("没有历史年级信息");
        return Res.ok(gradeService.listByIds(collect));
    }

}
